package org.eenie.autoapply;

import android.accessibilityservice.AccessibilityService;
import android.accessibilityservice.AccessibilityServiceInfo;
import android.app.Application;
import android.app.Service;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.IBinder;
import android.text.TextUtils;
import android.util.Log;
import android.view.accessibility.AccessibilityEvent;
import android.view.accessibility.AccessibilityNodeInfo;

import static android.view.accessibility.AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED;
import static android.view.accessibility.AccessibilityEvent.TYPE_WINDOWS_CHANGED;
import static android.view.accessibility.AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED;
import static android.view.accessibility.AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED;

public class AutoApplyService extends BaseAccessibilityService {


    private final String TAG = "AutoApply";
    String QQ_PKG = "com.tencent.mobileqq";



    public AutoApplyService() {
    }

    @Override
    public void onAccessibilityEvent(AccessibilityEvent event) {
//        Log.e(TAG, event.toString());


        if (event.getEventType() == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED) {
            //当窗口发生变化
            if (event.getClassName().toString().equals("com.tencent.av.ui.VideoInviteFull")) {
                final AccessibilityNodeInfo info = findViewByText("接听", true);
                AppApplication.postDelay(new Runnable() {
                    @Override
                    public void run() {
                        performViewClick(info);
                    }
                }, 3000);

            }
            if (event.getClassName().toString().equals("com.tencent.av.ui.AVActivity")) {

                final AccessibilityNodeInfo camera = findViewByText("打开摄像头", true);
                AppApplication.postDelay(new Runnable() {
                    @Override
                    public void run() {
                        performViewClick(camera);
                    }
                }, 1000);

//                if () {
//                }



//                if (camera.getContentDescription().equals("切换到后置摄像头")) {
//
//                    AppApplication.postDelay(new Runnable() {
//                        @Override
//                        public void run() {
//                            performViewClick(camera);
//                        }
//                    }, 1000);
//
//
//                }





//                for (int i = 0; i < getRootInActiveWindow().getChildCount(); i++) {
//                    AccessibilityNodeInfo childNode = getRootInActiveWindow().getChild(i);
//                    System.out.println(childNode);
//                    for (int k = 0; k < childNode.getChildCount(); k++) {
//                        Log.e(TAG, String.format("id = %s text = %s ", childNode.getChild(k).getClassName(), childNode.getChild(k).getText()));
//                        if (childNode.getChild(k).getText().equals("摄像头")) {
//                            cam
//                            break;
//                        }
//                    }
//                }

            }


            Log.e(TAG + " class =", event.getClassName().toString());

        }


        if (event.getEventType() == AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED) {
            for (int i = 0; i < getRootInActiveWindow().getChildCount(); i++) {
                final AccessibilityNodeInfo childNode = getRootInActiveWindow().getChild(i);
                if (childNode.getClassName().equals("切换到后置摄像头")) {
                    System.out.println(childNode);
                    Log.e(TAG, String.format("id = %s text = %s ", childNode.getClassName(), childNode.getContentDescription()));

                    AppApplication.postDelay(new Runnable() {
                        @Override
                        public void run() {
                            performViewClick(childNode);
                        }
                    }, 1000);
                }



            }
        }


    }

    @Override
    public void onInterrupt() {

    }


    @Override
    protected void onServiceConnected() {
        Log.e(TAG, "onServiceConnected");


        AccessibilityServiceInfo info = new AccessibilityServiceInfo();
        info.eventTypes = TYPE_WINDOW_CONTENT_CHANGED | TYPE_WINDOWS_CHANGED | TYPE_WINDOW_STATE_CHANGED | TYPE_NOTIFICATION_STATE_CHANGED;
        info.packageNames = new String[]{QQ_PKG};
        info.feedbackType = AccessibilityServiceInfo.FEEDBACK_SPOKEN;
        info.notificationTimeout = 1000;
        this.setServiceInfo(info);


    }
}
